package com.zny;

import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.shp.ShapeType;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GeometryList {
    /**
     * 获取shp几何对象集合
     *
     * @param fileUrl 文件地址
     * @return 几何集合
     */
    public static List<Geometry> getGeometryList(String fileUrl) throws IOException {
        List<Geometry> geometryList = new ArrayList<>();
        ShapefileReader r = null;
        try {
            r = new ShapefileReader(new ShpFiles(fileUrl), false, false, new GeometryFactory());
            while (r.hasNext()) {
                Geometry shape = (Geometry) r.nextRecord().shape();
                ShapeType shapeType = r.getHeader().getShapeType();
                double x1 = r.getHeader().maxX();
                double y1 = r.getHeader().maxY();
                double x2 = r.getHeader().minX();
                double y2 = r.getHeader().minY();

                geometryList.add(shape);
            }
        } catch (Exception e) {
            if (r != null) {
                r.close();
            }
            e.printStackTrace();
        }

        return geometryList;
    }
}
